from django.db import connection, connections
from trade import models
import math

def get_pages():
  """
  统计共有多少条数据，计算页面数量
  :return:
  """
  doamin_count = models.DomainList.objects.count()  # 统计有多少条数据
  pages = math.ceil(doamin_count / 30)  # 以每页30条显示，计算应该显示的页数
  return pages

def get_domian_info(p):
  """
  获取指定条数的信息，并处理
  :return:
  """
  #根据页获取30条数据
  p = int(p)
  pull_data_limit = p*30
  #sql语句获取数据
  cursor = connection.cursor()
  cursor.execute("SELECT * from trade_domainlist where id>=1 limit %r;" % (pull_data_limit,))
  row = cursor.fetchall()
  row_list = row[-30:]
  #处理数据
  domain_dic = {}
  for item in row_list:
    domain_id = item[0]
    domain_name = item[1]
    domain_info = eval(item[2])
    domain_name_len = len(domain_name.split('.')[0])
    domain_price = domain_info.get('价格')
    domain_score = domain_info.get('评分')
    buy_type = str(item[-3])
    domain_status = str(item[-1])
    domain_dic[domain_name] = {'id':domain_id, 'length': domain_name_len,
                                'price': domain_price,'status': domain_status,
                               'score':domain_score, 'type':buy_type}
  return domain_dic

def get_page_list(nowPage, totlePage):
  """
  获取一个页码表，渲染index页面
  :param nowPage: 当前的页码
  :param totlePage: 总页码
  :return: 返回一个页码列表
  """
  if nowPage < 5:
    if totlePage < 5:
      page_list = [i for i in range(1, totlePage+1)]
    else:
      page_list = [i for i in range(1,6)]
  elif nowPage >= totlePage - 2:
    page_list = [ totlePage-j  for j in range(4,-1,-1) ]
  else:
    page_list = [nowPage-k1 for k1 in range(3,1,-1)] + \
                [nowPage] + [nowPage+k2 for k2 in range(1,3)]
  return page_list

def option_doamin_info(nid,type_n=0):
  """
  根据ID处理域名的信息
  :param type: 1，返回域名交易的类型， 0，不返回域名交易的类型
  :param id: 域名的id号
  :return: 返回处理后的域名信息字典，域名交易类型
  """
  try:
    db_domain_info = models.DomainList.objects.filter(id=nid).first()
    db_domain_info_det = eval(db_domain_info.domain_info)
    domain_transaction = db_domain_info_det.get('交易类型')
    domain_info_dic = {
      'id': db_domain_info.id,
      'name': db_domain_info.domain_name,
      'info': db_domain_info_det,
      'status': db_domain_info.status
    }
  except AttributeError:
    return 'error', None
  if type_n == 1:
    return domain_info_dic,domain_transaction
  return domain_info_dic

def option_one_domain(nid):
  """
  细致 处理单条数据
  :param nid:
  :return: 处理后的字典对象
  """
  domain_info_dic = option_doamin_info(nid)
  info = domain_info_dic.get('info')
  info_dic = {
    'domainExplain': info.get('备案，文字说明'),
    'transactionType': info.get('交易类型'),
    'domainType': info.get('域名类型'),
    'domainScore': info.get('评分'),
    'domainEvaluation': info.get('估价'),
    'domainHistory': info.get('历史量'),
    'domainAlexa': info.get('alexa'),
    'AntiChain': info.get('反链'),
    'TheChain': info.get('外链'),
    'domainAmount': info.get('收录量'),
    'domainlLength': info.get('pr'),
    'domainPr': info.get('长度'),
    'domainWeight': info.get('权重'),
    'domainReTime': info.get('注册年限'),
    'RemainingTime': info.get('剩余时间'),
    'domainPrice': info.get('价格')
  }
  domain_info_dic['info'] = info_dic
  return domain_info_dic